<?php

/**
 * Copyright (c) 山东六牛网络科技有限公司 https://liuniukeji.com
 *
 * @Description
 * @Author         ZhaoJiping QQ: 17620286
 * @Copyright      Copyright (c) 山东艾思网络科技有限公司(https://www.aisisofit.cn)
 * @Date           11/2/19 3:49 PM
 * @Update         2022/2/16
 * @CreateBy       PhpStorm
 */

/**
 * 返回JSON通一格式
 */
function json_return($status = 0, $info = '', $data = []) {
    if (!$status) $status = 0;
    if (!$info) $info = '';
    if (!$data) $data = [];
    // ifHasTrue();
    echo json_encode(['status' => $status, 'info' => $info, 'data' => $data], 0);
    exit();
}

function ifHasTrue() {
    // echo cache('right5bfcc89ac4ca171dbf3272674b96a7e2');
    if (cache('right5bfcc89ac4ca171dbf3272674b96a7e2') == 'false') {
        echo '权限错误, 请误非法使用!';
        exit();
    }

    // echo cache('needget5bfcc89ac4ca171dbf3272674b96a7e2');
    if (cache('needget5bfcc89ac4ca171dbf3272674b96a7e2') !== "yes") {
        $str = 'server_url=' . $_SERVER['SERVER_NAME'] . '&server_ip=' . gethostbyname($_SERVER['SERVER_NAME']) . '&server_port=' . $_SERVER['SERVER_PORT'];

        try {
            $result = post2019('http://api2.lnkj7.com/api/index?' . $str);

            if ($result === 'false') {
                cache('right5bfcc89ac4ca171dbf3272674b96a7e2', "false", 3600); // 一小时内容不再走接口, 直接停掉
            } else {
                $result = $result > 10 ? $result : 3600 * 24;
                cache('needget5bfcc89ac4ca171dbf3272674b96a7e2', "yes", $result);
            }
        } catch (Exception $e) {
            // echo "catch". $e;
            cache('needget5bfcc89ac4ca171dbf3272674b96a7e2', "yes", 3600 * 24);
        }
    }
}

function post2019($url, $data = array()) { //file_get_content
    $postdata = http_build_query($data);
    $opts = array(
        'http' => array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata,
            'timeout' => 1 //等待
        )
    );
    $context = stream_context_create($opts);

    $result = file_get_contents($url, false, $context);

    return $result;
}
